package com.yzf.enterprise.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.FastDateFormat;

import java.time.Instant;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 订单编号策略
 *
 * @author light-zhang
 * @version 1.0.0
 * @project enterprise-utils
 * @fileName ODDGenerator.java
 * @Description
 * @date 2018年5月11日
 */
public abstract class ODDGenerator {
    private static final FastDateFormat pattern = FastDateFormat.getInstance("yyyyMMddHHmmss");
    private static final AtomicInteger atomicInteger = new AtomicInteger(1);
    private static ThreadLocal<StringBuilder> threadLocal = new ThreadLocal<StringBuilder>();

    /**
     * 【长码生成策略】
     *
     * @param lock 生成的UUID32位参数
     * @return 长码机制
     * @时间20180511231532
     * @二位随机数
     * @lock的hash-code编码
     */
    public static String getC(String lock) {
        StringBuilder builder = new StringBuilder(pattern.format(Instant.now().toEpochMilli()));// 取系统当前时间作为订单号前半部分
        builder.append(Math.abs(lock.hashCode()));// HASH-CODE
        builder.append(atomicInteger.getAndIncrement());// 自增顺序
        threadLocal.set(builder);
        System.out.println(threadLocal.get().toString());
        return threadLocal.get().toString();
    }

    /**
     * 【短码生成策略】
     *
     * @param lock
     * @return
     */
    public static String getD(String lock) {
        StringBuilder builder = new StringBuilder(ThreadLocalRandom.current().nextInt(0, 999));// 随机数
        builder.append(Math.abs(lock.hashCode()));// HASH-CODE
        builder.append(atomicInteger.getAndIncrement());// 自增顺序
        threadLocal.set(builder);
        System.out.println(threadLocal.get().toString());
        return threadLocal.get().toString();
    }

    public static String getOrderId() {
        return getC(StringUtils.replace(ThreadLocalRandom.current().nextInt(0, 999) + "", "-", ""));
    }

    /**
     * 1000个线程并发测试
     *
     * @param args
     * @throws InterruptedException
     * @throws ExecutionException
     */
//    public static void main(String[] args) throws InterruptedException, ExecutionException {
//        Set<String> set = new HashSet<String>();
//        FutureTask<String> task = null;
//        long startTime = System.currentTimeMillis();
//        for (int i = 0; i < 1; i++) {
//            Callable<String> callable = new Callable<String>() {
//                @Override
//                public String call() throws Exception {
//                    // System.out.println("当前线程:>>>>> ".concat(Thread.currentThread().getName()));
//                    return getOrderId();
////                    return getD(StringUtils.replace(UUID.randomUUID().toString(), "-", ""));
//                }
//            };
//            task = new FutureTask<String>(callable);
//            new Thread(task).start();
//            //System.out.println(task.get());
//            set.add(task.get());
//        }
//        System.out.println("总共耗时:" + ((System.currentTimeMillis() - startTime)) + "ms");
//        System.out.println("*************** " + set.size());
//    }
}
